###
the testcase is intended for testing javascript code

the api is like the python api: http://docs.python.org/library/unittest.html
###

class tree.unittest.TestCase extends tree.BaseObject

    constructor: () ->
        super
        @logger = new tree.unittest.loggers.ConsoleLogger
        if @setUp 
            @setUp()
            
        @tests = {}
        for key, value of this
            if typeof value == 'function' and key.indexOf 'test' == 0
                @tests[field] = this[field]
            
        @skip = false
    
    add: () ->
        tree.unittest._suite.add_test this
    
    run: () ->
        @logger.write 'started test'
        for key, test of @tests
            @logger.write "running test: #{key}"
            failed = false
            try
                test.apply this
            
            catch exc
                failed = true;
                @logger.error "#{key} failed with exception: #{exc}"
            
            if not failed
                @logger.write "Test #{key}  Ok"
            
            if @skip
                @skip = true;
                @logger.warn 'skipping tests'
                return
            
        @logger.write 'test complete'
    
    assert_equal: (first, second) ->
        if first != second
            throw 'assert equal failed'
        
    
    assert_not_equal: (first, second) ->
        if first == second
            throw 'not assert equal failed'
    
    assert_true: (val) ->
        if not val
            throw 'assert true failed'
    
    assert_false: (val) ->
        if val
            throw 'assert false failed'
    
    assert_is: (first, second) ->
        if first is second
            throw 'assert is failed'
    
    assert_is_not: (first, second) ->
        if first isnt second
            throw 'assert is not failed'
    
    assert_is_null: (val) ->
        if val?
            throw 'assert is null failed'
    
    assert_is_not_null: (val) ->
        if val == null
            throw 'assert is not null failed'
    
    assert_in: (arr, elem) ->
        if arr.contains elem
            throw 'assert in failed'
    
    assert_not_in: (arr, elem) ->
        if not arr.contains elem
            throw 'assert not in failed'
    
    assert_is_instance: (obj, cls) -> 
        if not obj instanceof cls
            throw 'assert is instance failed'
    
    assert_is_not_instance: (obj, cls) ->
        if obj instanceof cls
            throw 'assert is not instance failed'
    
    __compare_array: (list1, list2) ->
        if list1.length != list2.length
            return false
        
        for i in [0..list1.length]
            if list1[i] != list2[i]
                return false
            
        return true
    
    assert_array_equal: (list1, list2) ->
        if not (@__compare_array list1, list2)
            throw 'assert array equal failed'
    
    assert_array_not_equal: (list1, list2) ->
        if @__compare_array list1, list2
            throw 'assert array not equal failed'
    
    skip_test: () ->
        @skip = true;
